﻿#include "RenderWidgets.h"

#include "Render/VulkanMath.h" //应当移除掉,预渲染阶段不应当包含关于vulkan的任何APi
#include "Texture/Texture2D.h"
#include "Render/ForegroundMesh/RenderForegroundMeshInstance.h"

#include "Render/PreRenderContext.h"
#include "Render/PixelObject/RenderPixelObject.h"
#include "Render/PixelObject/PreRenderPixelObject.h"
#include "time/CPUTimeCounter.h"

void SRenderRectWidget::GeneratePreRenderWidgets(
    SPreRenderContext& InPreRenderContext
    ,CRenderSurface * InRenderSurface
    ,SPreRenderWidgets & OutPreRenderWidgets)
{

    //绘制内部矩形
    if(bFill)
    {
        vk::Rect2D InnerRect;
        if(BorderSize<=0)
        {
            InnerRect=CVulkanMath::ToVulkanRect2D(Rect);
        }
        else
        {
            InnerRect.offset.x = Rect.Min.X + BorderSize;
            InnerRect.offset.y = Rect.Min.Y + BorderSize;
            InnerRect.extent.width = Rect.Extent.X - 2 * BorderSize;
            InnerRect.extent.height = Rect.Extent.Y - 2 * BorderSize;
        }  

        if(InnerRect.extent.width>0
        && InnerRect.extent.height>0)
        {
            if(Radius-BorderSize>0)
            {
                float InnerRadius = Radius-BorderSize;

                //有圆角
                //将非圆角部分分为上中下三个部分
                //上部分
                auto& TopRect=OutPreRenderWidgets.RectWidgets.Emplace();
                TopRect.Viewport =vk::Viewport()
                .setX(InnerRadius+InnerRect.offset.x)
                .setY(InnerRect.offset.y)
                .setWidth(InnerRect.extent.width-2*InnerRadius)
                .setHeight(InnerRadius)
                .setMinDepth(0.0f)
                .setMaxDepth(1.f);
                TopRect.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);

                TopRect.Depth=ToDepth();
                TopRect.Color = FillColor;

                //中部分
                auto& MiddleRect=OutPreRenderWidgets.RectWidgets.Emplace();
                MiddleRect.Viewport =vk::Viewport()
                .setX(InnerRect.offset.x)
                .setY(InnerRect.offset.y+InnerRadius)
                .setWidth(InnerRect.extent.width)
                .setHeight(InnerRect.extent.height-2*InnerRadius)
                .setMinDepth(0.0f)
                .setMaxDepth(1.f);
                MiddleRect.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);
                MiddleRect.Depth=ToDepth();
                MiddleRect.Color = FillColor;

                //下部分
                auto& BottomRect=OutPreRenderWidgets.RectWidgets.Emplace();
                BottomRect.Viewport =vk::Viewport()
                .setX(InnerRadius+InnerRect.offset.x)
                .setY(InnerRect.offset.y+InnerRect.extent.height-InnerRadius)
                .setWidth(InnerRect.extent.width-2*InnerRadius)
                .setHeight(InnerRadius)
                .setMinDepth(0.0f)
                .setMaxDepth(1.f);
                BottomRect.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);
                BottomRect.Depth=ToDepth();
                BottomRect.Color = FillColor;

                //四个圆角
                auto& TopLeftCircle=OutPreRenderWidgets.CircleWidgets.Emplace();
                TopLeftCircle.Viewport =vk::Viewport()
                .setX(InnerRect.offset.x)
                .setY(InnerRect.offset.y)
                .setWidth(InnerRadius)
                .setHeight(InnerRadius)
                .setMinDepth(0.0f)
                .setMaxDepth(1.f);
                TopLeftCircle.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);
                TopLeftCircle.Depth=ToDepth();

                TopLeftCircle.Center=SVec2(InnerRect.offset.x+InnerRadius,InnerRect.offset.y+InnerRadius);
                TopLeftCircle.Radius=InnerRadius;
                TopLeftCircle.Color = FillColor;

                auto& TopRightCircle=OutPreRenderWidgets.CircleWidgets.Emplace();
                TopRightCircle.Viewport =vk::Viewport()
                .setX(InnerRect.offset.x+InnerRect.extent.width-InnerRadius)
                .setY(InnerRect.offset.y)
                .setWidth(InnerRadius)
                .setHeight(InnerRadius)
                .setMinDepth(0.0f)
                .setMaxDepth(1.f);
                TopRightCircle.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);
                TopRightCircle.Depth=ToDepth();

                TopRightCircle.Center=SVec2(InnerRect.offset.x+InnerRect.extent.width-InnerRadius,InnerRect.offset.y+InnerRadius);
                TopRightCircle.Radius=InnerRadius;
                TopRightCircle.Color = FillColor;

                auto& BottomLeftCircle=OutPreRenderWidgets.CircleWidgets.Emplace();
                BottomLeftCircle.Viewport =vk::Viewport()
                .setX(InnerRect.offset.x)
                .setY(InnerRect.offset.y+InnerRect.extent.height-InnerRadius)
                .setWidth(InnerRadius)
                .setHeight(InnerRadius)
                .setMinDepth(0.0f)
                .setMaxDepth(1.f);
                BottomLeftCircle.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);
                BottomLeftCircle.Depth=ToDepth();

                BottomLeftCircle.Center=SVec2(InnerRect.offset.x+InnerRadius,InnerRect.offset.y+InnerRect.extent.height-InnerRadius);
                BottomLeftCircle.Radius=InnerRadius;
                BottomLeftCircle.Color = FillColor;

                auto& BottomRightCircle=OutPreRenderWidgets.CircleWidgets.Emplace();
                BottomRightCircle.Viewport =vk::Viewport()
                .setX(InnerRect.offset.x+InnerRect.extent.width-InnerRadius)
                .setY(InnerRect.offset.y+InnerRect.extent.height-InnerRadius)
                .setWidth(InnerRadius)
                .setHeight(InnerRadius)
                .setMinDepth(0.0f)
                .setMaxDepth(1.f);
                BottomRightCircle.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);

                BottomRightCircle.Depth=ToDepth();

                BottomRightCircle.Center=SVec2(InnerRect.offset.x+InnerRect.extent.width-InnerRadius,InnerRect.offset.y+InnerRect.extent.height-InnerRadius);
                BottomRightCircle.Radius=InnerRadius;
                BottomRightCircle.Color = FillColor;


            }
            else
            {
                //没有圆角
                auto& Rect=OutPreRenderWidgets.RectWidgets.Emplace();
                Rect.Viewport =vk::Viewport()
                .setX(InnerRect.offset.x)
                .setY(InnerRect.offset.y)
                .setWidth(InnerRect.extent.width)
                .setHeight(InnerRect.extent.height)
                .setMinDepth(0.0f)
                .setMaxDepth(1.f);
                Rect.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);

                Rect.Depth=ToDepth();
                
                Rect.Color = FillColor;

            }
           
        }

    }

    //绘制边框
    if(BorderSize>0)
    {
        if(Radius)
        {
            //有圆角
            //将非圆角部分分为上下左右四个部分
            //上部分
            auto& TopRect=OutPreRenderWidgets.RectWidgets.Emplace();
            TopRect.Viewport =vk::Viewport()
            .setX(Rect.Min.X+Radius)
            .setY(Rect.Min.Y)
            .setWidth(Rect.Extent.X-2*Radius)
            .setHeight(BorderSize)
            .setMinDepth(0.0f)
            .setMaxDepth(1.f);
            TopRect.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);

            TopRect.Depth=ToDepth();

            TopRect.Color = BorderColor;

            //下部分
            auto& BottomRect=OutPreRenderWidgets.RectWidgets.Emplace();
            BottomRect.Viewport =vk::Viewport()
            .setX(Rect.Min.X+Radius)
            .setY(Rect.Min.Y+Rect.Extent.Y-BorderSize)
            .setWidth(Rect.Extent.X-2*Radius)
            .setHeight(BorderSize)
            .setMinDepth(0.0f)
            .setMaxDepth(1.f);
            BottomRect.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);
            BottomRect.Depth=ToDepth();

            BottomRect.Color = BorderColor;

            //左部分
            auto& LeftRect=OutPreRenderWidgets.RectWidgets.Emplace();
            LeftRect.Viewport =vk::Viewport()
            .setX(Rect.Min.X)
            .setY(Rect.Min.Y+Radius)
            .setWidth(BorderSize)
            .setHeight(Rect.Extent.Y-2*Radius)
            .setMinDepth(0.0f)
            .setMaxDepth(1.f);
            LeftRect.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);
            LeftRect.Depth=ToDepth();

            LeftRect.Color = BorderColor;

            //右部分
            auto& RightRect=OutPreRenderWidgets.RectWidgets.Emplace();
            RightRect.Viewport =vk::Viewport()
            .setX(Rect.Min.X+Rect.Extent.X-BorderSize)
            .setY(Rect.Min.Y+Radius)
            .setWidth(BorderSize)
            .setHeight(Rect.Extent.Y-2*Radius)
            .setMinDepth(0.0f)
            .setMaxDepth(1.f);
            RightRect.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);

            RightRect.Depth=ToDepth();

            RightRect.Color = BorderColor;

            //四个圆角
            auto& TopLeftRing=OutPreRenderWidgets.RingWidgets.Emplace();
            TopLeftRing.Viewport =vk::Viewport()
            .setX(Rect.Min.X)
            .setY(Rect.Min.Y)
            .setWidth(Radius)
            .setHeight(Radius)
            .setMinDepth(0.0f)
            .setMaxDepth(1.f);
            TopLeftRing.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);
            TopLeftRing.Depth=ToDepth();

            TopLeftRing.Center=SVec2(Rect.Min.X+Radius,Rect.Min.Y+Radius);
            TopLeftRing.Radius=Radius;
            TopLeftRing.Width=BorderSize;
            TopLeftRing.Color = BorderColor;

            auto& TopRightRing=OutPreRenderWidgets.RingWidgets.Emplace();
            TopRightRing.Viewport =vk::Viewport()
            .setX(Rect.Min.X+Rect.Extent.X-Radius)
            .setY(Rect.Min.Y)
            .setWidth(Radius)
            .setHeight(Radius)
            .setMinDepth(0.0f)
            .setMaxDepth(1.f);
            TopRightRing.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);

            TopRightRing.Depth=ToDepth();

            TopRightRing.Center=SVec2(Rect.Min.X+Rect.Extent.X-Radius,Rect.Min.Y+Radius);
            TopRightRing.Radius=Radius;
            TopRightRing.Width=BorderSize;
            TopRightRing.Color = BorderColor;

            auto& BottomLeftRing=OutPreRenderWidgets.RingWidgets.Emplace();
            BottomLeftRing.Viewport =vk::Viewport()
            .setX(Rect.Min.X)
            .setY(Rect.Min.Y+Rect.Extent.Y-Radius)
            .setWidth(Radius)
            .setHeight(Radius)
            .setMinDepth(0.0f)
            .setMaxDepth(1.f);
            BottomLeftRing.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);

            BottomLeftRing.Depth=ToDepth();

            BottomLeftRing.Center=SVec2(Rect.Min.X+Radius,Rect.Min.Y+Rect.Extent.Y-Radius);
            BottomLeftRing.Radius=Radius;
            BottomLeftRing.Width=BorderSize;
            BottomLeftRing.Color = BorderColor;

            auto& BottomRightRing=OutPreRenderWidgets.RingWidgets.Emplace();
            BottomRightRing.Viewport =vk::Viewport()
            .setX(Rect.Min.X+Rect.Extent.X-Radius)
            .setY(Rect.Min.Y+Rect.Extent.Y-Radius)
            .setWidth(Radius)
            .setHeight(Radius)
            .setMinDepth(0.0f)
            .setMaxDepth(1.f);
            BottomRightRing.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);

            BottomRightRing.Depth=ToDepth();

            BottomRightRing.Center=SVec2(Rect.Min.X+Rect.Extent.X-Radius,Rect.Min.Y+Rect.Extent.Y-Radius);
            BottomRightRing.Radius=Radius;
            BottomRightRing.Width=BorderSize;
            BottomRightRing.Color = BorderColor;
        }
        else
        {
            //没有圆角
            //分为上下左右四个部分
            //上部分
            auto& TopRect=OutPreRenderWidgets.RectWidgets.Emplace();
            TopRect.Viewport =vk::Viewport()
            .setX(Rect.Min.X)
            .setY(Rect.Min.Y)
            .setWidth(Rect.Extent.X)
            .setHeight(BorderSize)
            .setMinDepth(0.0f)
            .setMaxDepth(1.f);
            TopRect.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);
            TopRect.Depth=ToDepth();
            
            TopRect.Color = BorderColor;
        
            //下部分
            auto& BottomRect=OutPreRenderWidgets.RectWidgets.Emplace();
            BottomRect.Viewport =vk::Viewport()
            .setX(Rect.Min.X)
            .setY(Rect.Min.Y+Rect.Extent.Y-BorderSize)
            .setWidth(Rect.Extent.X)
            .setHeight(BorderSize)
            .setMinDepth(0.0f)
            .setMaxDepth(1.f);
            BottomRect.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);
            BottomRect.Depth=ToDepth();

            BottomRect.Color = BorderColor;

            //左部分
            auto& LeftRect=OutPreRenderWidgets.RectWidgets.Emplace();
            LeftRect.Viewport =vk::Viewport()
            .setX(Rect.Min.X)
            .setY(Rect.Min.Y+BorderSize)
            .setWidth(BorderSize)
            .setHeight(Rect.Extent.Y-2*BorderSize)
            .setMinDepth(0.0f)
            .setMaxDepth(1.f);
            LeftRect.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);

            LeftRect.Depth=ToDepth();

            LeftRect.Color = BorderColor;

            //右部分
            auto& RightRect=OutPreRenderWidgets.RectWidgets.Emplace();
            RightRect.Viewport =vk::Viewport()
            .setX(Rect.Min.X+Rect.Extent.X-BorderSize)
            .setY(Rect.Min.Y+BorderSize)
            .setWidth(BorderSize)
            .setHeight(Rect.Extent.Y-2*BorderSize)
            .setMinDepth(0.0f)
            .setMaxDepth(1.f);
            RightRect.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);

            RightRect.Depth=ToDepth();

            RightRect.Color = BorderColor;
        
        }

    }


}

void SRenderImageWidget::GeneratePreRenderWidgets(SPreRenderContext& InPreRenderContext,CRenderSurface *InRenderSurface, SPreRenderWidgets &OutPreRenderWidgets)
{
    if(!Image)
    {
        return;
    }

    switch(ScaleMethod)
    {
        case EImageWidgetScaleMethod::NoScale:
        {   
            auto FinalClip=ClipRect.Intersect(Rect);
            if(!FinalClip.HasValue())
            {
                return;
            }

            auto& ImageWidget=OutPreRenderWidgets.ImageWidgets.Emplace();
            ImageWidget.TintColor=TintColor;
            ImageWidget.Depth=ToDepth();
            ImageWidget.Image=InPreRenderContext.PreRender(Image);
            ImageWidget.Viewport =vk::Viewport()
            .setX(Rect.Min.X)
            .setY(Rect.Min.Y)
            .setWidth(Image->GetWidth())
            .setHeight(Image->GetHeight())
            .setMinDepth(0.0f)
            .setMaxDepth(1.f);
            ImageWidget.LeftTopUV=SVec2(0,0);
            ImageWidget.RightBottomUV=SVec2(1,1);

            ImageWidget.Scissor=CVulkanMath::ToVulkanRect2D(FinalClip.GetValue());

            break;
        }
        case EImageWidgetScaleMethod::Fill:
        {
            auto& ImageWidget=OutPreRenderWidgets.ImageWidgets.Emplace();
            ImageWidget.TintColor=TintColor;
            ImageWidget.Depth=ToDepth();
            ImageWidget.Image=InPreRenderContext.PreRender(Image);
            ImageWidget.Viewport =vk::Viewport()
            .setX(Rect.Min.X)
            .setY(Rect.Min.Y)
            .setWidth(Rect.Extent.X)
            .setHeight(Rect.Extent.Y)
            .setMinDepth(0.0f)
            .setMaxDepth(1.f);
            ImageWidget.LeftTopUV=SVec2(0,0);
            ImageWidget.RightBottomUV=SVec2(1,1);

            ImageWidget.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);
            break;
        }

        case EImageWidgetScaleMethod::Tile:
        {
            auto& ImageWidget=OutPreRenderWidgets.ImageWidgets.Emplace();
            ImageWidget.TintColor=TintColor;
            ImageWidget.Depth=ToDepth();
            ImageWidget.Image=InPreRenderContext.PreRender(Image);
            ImageWidget.Viewport =vk::Viewport()
            .setX(Rect.Min.X)
            .setY(Rect.Min.Y)
            .setWidth(Rect.Extent.X)
            .setHeight(Rect.Extent.Y)
            .setMinDepth(0.0f)
            .setMaxDepth(1.f);
            ImageWidget.LeftTopUV=SVec2(0,0);
            ImageWidget.RightBottomUV=SVec2(Rect.Extent.X/Image->GetWidth(),Rect.Extent.Y/Image->GetHeight());

            ImageWidget.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);
            break;
        }

        case EImageWidgetScaleMethod::Custom:
        {
            auto& ImageWidget=OutPreRenderWidgets.ImageWidgets.Emplace();
            ImageWidget.TintColor=TintColor;
            ImageWidget.Depth=ToDepth();
            ImageWidget.Image=InPreRenderContext.PreRender(Image);
            ImageWidget.Viewport =vk::Viewport()
            .setX(Rect.Min.X)
            .setY(Rect.Min.Y)
            .setWidth(Rect.Extent.X)
            .setHeight(Rect.Extent.Y)
            .setMinDepth(0.0f)
            .setMaxDepth(1.f);
            ImageWidget.LeftTopUV=CustomLeftTopUV;
            ImageWidget.RightBottomUV=CustomRightBottomUV;

            ImageWidget.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);
            break;
        }


        default:
        {
            //unreachable
            assert(false);
            break;
        }
    }

}

// void SRenderTextWidget::GeneratePreRenderWidgets(SPreRenderContext& InPreRenderContext,CRenderSurface *InRenderSurface, SPreRenderWidgets &OutPreRenderWidgets)
// {
//     float Depth=ToDepth();
//     auto Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);

//     SPreRenderTextWidget TextWidget;
//     TextWidget.CharData.Reserve(CharColors.Num());

//     for(int64_t i=0;i<CharColors.Num();++i)
//     {
//         auto& CharData=TextWidget.CharData.Emplace();
//         CharColors[i].ToFloatArray(CharData.Color);
//         CharData.ZOrder=Depth;

//         CharData.LeftTopUV[0]= CharPositions[i].X /Rect.Extent.X;
//         CharData.LeftTopUV[1]= CharPositions[i].Y /Rect.Extent.Y;

//         CharData.RightBottomUV[0]= (CharPositions[i].X+TextImage->GetWidth()) /Rect.Extent.X;
//         CharData.RightBottomUV[1]= (CharPositions[i].Y+TextImage->GetHeight()) /Rect.Extent.Y;

//         CharData.Padding[0]=0;
//         CharData.Padding[1]=0;
//         CharData.Padding[2]=0;
//     }

//     if(TextWidget.CharData.Empty())
//     {
//         //没有字符
//         return;
//     }
//     TextWidget.TextImage=InPreRenderContext.PreRender(TextImage);
//     TextWidget.Depth=Depth;
//     TextWidget.Scissor=Scissor;
//     TextWidget.Viewport =vk::Viewport()
//         .setX(Rect.Min.X)
//         .setY(Rect.Min.Y)
//         .setWidth(Rect.Extent.X)
//         .setHeight(Rect.Extent.Y)
//         .setMinDepth(0.0f)
//         .setMaxDepth(1.f);

//     //检查数据是否合法

//     if(TextWidget.Viewport.width<=0 || TextWidget.Viewport.height<=0)
//     {
//         return;
//     }

//     //检查裁剪是否合法
//     if(TextWidget.Scissor.extent.width<=0 || TextWidget.Scissor.extent.height<=0)
//     {
//         return;
//     }

//     //如果裁剪矩形完全在视口外面，则不需要绘制
//     if(TextWidget.Scissor.offset.x+TextWidget.Scissor.extent.width<=TextWidget.Viewport.x ||
//         TextWidget.Scissor.offset.x>=TextWidget.Viewport.x+TextWidget.Viewport.width ||
//         TextWidget.Scissor.offset.y+TextWidget.Scissor.extent.height<=TextWidget.Viewport.y ||
//         TextWidget.Scissor.offset.y>=TextWidget.Viewport.y+TextWidget.Viewport.height)
//     {
//         return;
//     }


//     OutPreRenderWidgets.TextWidgets.Add(TextWidget) ;

// }

void SRenderViewportWidget::GeneratePreRenderWidgets(SPreRenderContext& InPreRenderContext,CRenderSurface *InRenderSurface, SPreRenderWidgets &OutPreRenderWidgets)
{
    if(Scenes.Empty())
    {
        return;
    }


    auto& ViewportWidget=OutPreRenderWidgets.ViewportWidgets.Emplace();
    ViewportWidget.Depth=ToDepth();
    ViewportWidget.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);
    ViewportWidget.Viewport =vk::Viewport()
    .setX(Rect.Min.X)
    .setY(Rect.Min.Y)
    .setWidth(Rect.Extent.X)
    .setHeight(Rect.Extent.Y)
    .setMinDepth(0.0f)
    .setMaxDepth(1.f);

    for(auto& Scene:Scenes)
    {
        auto& PreRenderScene=ViewportWidget.Scenes.Emplace();
        Scene->GeneratePreRenderScene(InPreRenderContext,&PreRenderScene);
    }
    ViewportWidget.CameraParameters=CameraParameters;
    ViewportWidget.AntiAliasingMethod=AntiAliasingMethod;

    for(auto& ForegroundMeshInstance:ForegroundMeshInstances)
    {
        auto& ForegroundMeshInstanceData=ViewportWidget.ForegroundMeshInstances.Emplace();
        ForegroundMeshInstance->GeneratePreRenderData(InPreRenderContext,ForegroundMeshInstanceData);
    }

    if(PixelObjects)
    {
        ViewportWidget.PixelObjects=std::make_shared<CPreRenderPixelObjectGroup>();
        PixelObjects->GeneratePreRenderData(InPreRenderContext,ViewportWidget.PixelObjects);
    }
}

void SRenderTriangleWidget::GeneratePreRenderWidgets(SPreRenderContext& InPreRenderContext,CRenderSurface *InRenderSurface, SPreRenderWidgets &OutPreRenderWidgets)
{
    auto& TriangleWidget=OutPreRenderWidgets.TriangleWidgets.Emplace();
    TriangleWidget.Depth=ToDepth();
    TriangleWidget.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);
    TriangleWidget.Viewport =vk::Viewport()
    .setX(Rect.Min.X)
    .setY(Rect.Min.Y)
    .setWidth(Rect.Extent.X)
    .setHeight(Rect.Extent.Y)
    .setMinDepth(0.0f)
    .setMaxDepth(1.f);

    TriangleWidget.Color=Color;
    TriangleWidget.P0=P0;
    TriangleWidget.P1=P1;
    TriangleWidget.P2=P2;
}

void SRenderLineWidget::GeneratePreRenderWidgets(SPreRenderContext& InPreRenderContext,CRenderSurface *InRenderSurface, SPreRenderWidgets &OutPreRenderWidgets)
{
    auto& LineWidget=OutPreRenderWidgets.LineWidgets.Emplace();
    LineWidget.Depth=ToDepth();
    LineWidget.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);
    LineWidget.Viewport =vk::Viewport()
    .setX(Rect.Min.X)
    .setY(Rect.Min.Y)
    .setWidth(Rect.Extent.X)
    .setHeight(Rect.Extent.Y)
    .setMinDepth(0.0f)
    .setMaxDepth(1.f);

    LineWidget.Color=Color;
    LineWidget.Start=Start;
    LineWidget.End=End;
    LineWidget.Width=Width;
}

void SRenderCircleWidget::GeneratePreRenderWidgets(SPreRenderContext& InPreRenderContext,CRenderSurface *InRenderSurface, SPreRenderWidgets &OutPreRenderWidgets)
{
    auto& CircleWidget=OutPreRenderWidgets.CircleWidgets.Emplace();
    CircleWidget.Depth=ToDepth();
    CircleWidget.Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);
    CircleWidget.Viewport =vk::Viewport()
    .setX(Rect.Min.X)
    .setY(Rect.Min.Y)
    .setWidth(Rect.Extent.X)
    .setHeight(Rect.Extent.Y)
    .setMinDepth(0.0f)
    .setMaxDepth(1.f);

    CircleWidget.Color=Color;
    CircleWidget.Center=Center+Rect.Min;
    CircleWidget.Radius=Radius;
}

void SRenderStaticTextWidget::GeneratePreRenderWidgets(SPreRenderContext &InPreRenderContext, CRenderSurface *InRenderSurface, SPreRenderWidgets &OutPreRenderWidgets)
{
    //将字符按照纹理分类
    TMap<std::shared_ptr<CRenderTexture2D>,TVector<int32_t>> TextImageIndices;
    for(int32_t i=0;i<CharTextures.Num();++i)
    {
        auto& CharTexture=CharTextures[i];
        if(!TextImageIndices.Contains(CharTexture.Texture))
        {
            TextImageIndices.Add(CharTexture.Texture,TVector<int32_t>());
        }
        TextImageIndices[CharTexture.Texture].Add(i);
    }

    auto Depth=ToDepth();
    auto Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);
    auto Viewport =vk::Viewport()
    .setX(Rect.Min.X)
    .setY(Rect.Min.Y)
    .setWidth(Rect.Extent.X)
    .setHeight(Rect.Extent.Y)
    .setMinDepth(0.0f)
    .setMaxDepth(1.f);

    for(auto& Pair:TextImageIndices)
    {
        auto& TextWidget=OutPreRenderWidgets.StaticTextWidgets.Emplace();
        TextWidget.Scissor=Scissor;
        TextWidget.Viewport =Viewport;
        TextWidget.Depth=Depth;
        TextWidget.TextColor=TextColor;
        TextWidget.TextImage=InPreRenderContext.PreRender(Pair.Key);

        for(auto Index:Pair.Value)
        {
            auto& CharData=TextWidget.CharData.Emplace();
            //CharData.ZOrder=Depth;

            CharTextures[Index].LeftTopUV.ToFloatArray(CharData.LeftTopUV);
            CharTextures[Index].RightBottomUV.ToFloatArray(CharData.RightBottomUV);

            CharData.LeftTopPosition[0]=(CharPositions[Index].X /Rect.Extent.X)*2-1;
            CharData.LeftTopPosition[1]=(CharPositions[Index].Y /Rect.Extent.Y)*2-1;

            CharData.RightBottomPosition[0]= ((CharPositions[Index].X+CharTextures[Index].Size.X) /Rect.Extent.X)*2-1;
            CharData.RightBottomPosition[1]= ((CharPositions[Index].Y+CharTextures[Index].Size.Y) /Rect.Extent.Y)*2-1;

            // CharData.Padding[0]=0;
            // CharData.Padding[1]=0;
            // CharData.Padding[2]=0;
        }
    }
}

void SRenderDynamicTextWidget::GeneratePreRenderWidgets(SPreRenderContext &InPreRenderContext, CRenderSurface *InRenderSurface, SPreRenderWidgets &OutPreRenderWidgets)
{
    CODE_BLOCK_TIME_COUNT(U"动态文本预渲染")

    //将字符按照纹理分类
    TMap<std::shared_ptr<CRenderTexture2D>,TVector<int32_t>> TextImageIndices(CharTextures.Num());

    for(int32_t i=0;i<CharTextures.Num();++i)
    {
        auto& CharTexture=CharTextures[i];
        if(!TextImageIndices.Contains(CharTexture.Texture))
        {
            TextImageIndices.Add(CharTexture.Texture,TVector<int32_t>());
        }
        TextImageIndices[CharTexture.Texture].Add(i);
    }

    auto Depth=ToDepth();
    auto Scissor=CVulkanMath::ToVulkanRect2D(ClipRect);
    auto Viewport =vk::Viewport()
    .setX(Rect.Min.X)
    .setY(Rect.Min.Y)
    .setWidth(Rect.Extent.X)
    .setHeight(Rect.Extent.Y)
    .setMinDepth(0.0f)
    .setMaxDepth(1.f);


    TVector<SPreRenderTextWidget> TextWidgets;
    TextWidgets.Reserve(TextImageIndices.Num());
    for(auto& Pair:TextImageIndices)
    {
        auto& TextWidget=TextWidgets.Emplace();
        TextWidget.Scissor=Scissor;
        TextWidget.Viewport =Viewport;
        TextWidget.Depth=Depth;
        //TextWidget.TextColor=TextColor;
        TextWidget.TextImage=InPreRenderContext.PreRender(Pair.Key);

        TextWidget.CharData.Reserve(Pair.Value.Num());
        for(auto Index:Pair.Value)
        {
            auto& CharData=TextWidget.CharData.Emplace();
            CharColors[Index].ToFloatArray(CharData.Color);
            //CharData.ZOrder=Depth;

            CharTextures[Index].LeftTopUV.ToFloatArray(CharData.LeftTopUV);
            CharTextures[Index].RightBottomUV.ToFloatArray(CharData.RightBottomUV);

            CharData.LeftTopPosition[0]=(CharPositions[Index].X /Rect.Extent.X)*2-1;
            CharData.LeftTopPosition[1]=(CharPositions[Index].Y /Rect.Extent.Y)*2-1;
            CharData.RightBottomPosition[0]= ((CharPositions[Index].X+CharTextures[Index].Size.X) /Rect.Extent.X)*2-1;
            CharData.RightBottomPosition[1]= ((CharPositions[Index].Y+CharTextures[Index].Size.Y) /Rect.Extent.Y)*2-1;


            // CharData.Padding[0]=0;
            // CharData.Padding[1]=0;
            // CharData.Padding[2]=0;
        }
    }
    OutPreRenderWidgets.TextWidgets.Append(TextWidgets);
}
